package com.frogs.ucenter.controller;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.ucenter.entity.IntegralConsumeRelation;
import com.frogs.ucenter.service.IntegralConsumeRelationService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin/integralConsumeRelation")
public class IntegralConsumeRelationController extends BaseController3 {
    private static final String ROOT_VIEW = "admin/integralConsumeRelation";

    private static final String HOME_VIEW = "admin/integralConsumeRelation/home";

    private static final String EDIT_VIEW = "admin/integralConsumeRelation/edit";

    @Autowired
    private IntegralConsumeRelationService integralConsumeRelationService;

    @RequestMapping()
    public String listIntegralConsumeRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createIntegralConsumeRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.put( "model",integralConsumeRelationService.getById( id ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveIntegralConsumeRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, IntegralConsumeRelation integralConsumeRelation) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            integralConsumeRelationService.insert( integralConsumeRelation ); 
            builder = DWZResponse.getSucessBuilder("新增成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("新增失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(SHOW)
    public String showIntegralConsumeRelation(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            model.addAttribute( integralConsumeRelationService.getById( id ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateIntegralConsumeRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, IntegralConsumeRelation integralConsumeRelationNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        boolean flag = true;
        if( id > 0 ){
            DWZResponse.Builder builder;
            try {
                IntegralConsumeRelation integralConsumeRelation = integralConsumeRelationService.getById( id ); 
                PropertyUtils.copyProperties(integralConsumeRelation, integralConsumeRelationNew);
                integralConsumeRelationService.update( integralConsumeRelation ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
            	flag = false;
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        request.setAttribute("flag", flag);
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteIntegralConsumeRelation(HttpServletRequest request, HttpServletResponse response, ModelMap model, @RequestParam(required=true, value="ids")String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        boolean flag = true;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            integralConsumeRelationService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除成功");
        } catch (Exception e) {
        	flag = false;
            builder = DWZResponse.getFailBuilder("删除失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        request.setAttribute("flag", flag);
        return null;
    }

    private PageList<IntegralConsumeRelation> getPageList(ModelMap model) {
        PageList<IntegralConsumeRelation> integralConsumeRelationList = integralConsumeRelationService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("integralConsumeRelationList",integralConsumeRelationList);
        model.put("pageTurn",integralConsumeRelationList.getPageTurn());
        return integralConsumeRelationList;
    }
}